<template>
  <div class="setting">
    <el-tabs>
      <el-tab-pane label="公司设置">
        <el-button size="small" type="primary" @click="add"
          >+ 新增角色</el-button
        >
        <el-table :data="list">
          <el-table-column label="序号">
            <template v-slot="{ $index }">
              <div>
                {{ (page.page - 1) * page.pagesize + $index + 1 }}
              </div>
            </template>
          </el-table-column>
          <el-table-column label="角色名" prop="name"></el-table-column>
          <el-table-column label="描述" prop="description"></el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row }">
              <div>
                <el-button type="text" @click="setPower(row)">分配权限</el-button>
                <el-button type="text" @click="edit(row)">修改</el-button>
                <el-button type="text" @click="del(row.id)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            @size-change="pageSizeChange"
            @current-change="pageChange"
            :current-page="page.page"
            :page-sizes="[1, 5, 10, 100, 200]"
            :page-size="page.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </div>
      </el-tab-pane>
      <!-- 公司信息 -->
      <el-tab-pane label="公司信息" lazy>
        <component :is="'company'"></component> </el-tab-pane>
    </el-tabs>
    <DialogItem
      :isShow.sync="show"
      :list="list"
      @pageChange="pageChange"
      ref="dialogItem"
      @getData="getData"
    ></DialogItem>
    <!-- 分配权限 -->
    <DialogPower :isShow.sync="showDialog" ref="dialogPower"></DialogPower>
  </div>
</template>

<script>
import { sysRole, sysRoleDel } from '@/api/setting'
import DialogItem from './components/dialog-item.vue'
import DialogPower from './components/dialogPower.vue'
export default {
  components: {
    DialogItem,
    DialogPower,
    Company: () => import('./components/company.vue')
  },
  data () {
    return {
      show: false,
      showDialog:false,
      list: [],
      page: {
        page: 1,
        pagesize: 5
      },
      total: 0,
      form: {
        name: '',
        description: ''
      }
    }
  },
  created () {
    this.getData()
  },
  methods: {
    async getData () {
      const res = await sysRole(this.page)
      this.list = res.data.rows
      this.total = res.data.total
      console.log(res)
    },
    // 页码改变
    pageChange (page) {
      this.page.page = page
      this.getData()
    },
    // 页容量改变
    pageSizeChange (size) {
      this.page.pagesize = size
      this.page.page = 1
      this.getData()
    },
    // 新增角色
    add () {
      this.show = true
      this.$refs.dialogItem.mode = 'add'
    },
    // 删除角色
    del (id) {
      this.$confirm('确定要删除本条内容吗?', '提示')
        .then(async () => {
          await sysRoleDel(id)
          this.$message.success('删除成功')
          this.pageChange(1)
        })
        .catch(() => {})
    },
    // 编辑
    edit (row) {
      this.show = true
      this.$refs.dialogItem.mode = 'edit'
      this.$refs.dialogItem.form = JSON.parse(JSON.stringify(row))
    },
    // 分配权限
    setPower(row){
      this.showDialog =true
      this.$refs.dialogPower.getPowerAll()
      this.$refs.dialogPower.getData(row.id)
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 15px;
}
.pagination {
  text-align: center;
  padding-top: 10px;
}
</style>
